//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.yang.sofa;

import com.alipay.sofa.boot.util.NamedThreadFactory;
import com.alipay.sofa.common.thread.SofaThreadPoolExecutor;
import com.alipay.sofa.runtime.log.SofaLogger;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Future;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import org.springframework.core.env.Environment;

public class AsyncTaskExecutor {
    protected static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
    protected static final AtomicReference<ThreadPoolExecutor> THREAD_POOL_REF = new AtomicReference();
    protected static final List<Future> FUTURES = new ArrayList();
    protected static final AtomicBoolean STARTED = new AtomicBoolean(false);

    public AsyncTaskExecutor() {
    }

    public static Future submitTask(Environment environment, Runnable runnable) {
        if (THREAD_POOL_REF.get() == null) {
            ThreadPoolExecutor threadPoolExecutor = createThreadPoolExecutor(environment);
            boolean success = THREAD_POOL_REF.compareAndSet((Object)null, threadPoolExecutor);
            if (!success) {
                threadPoolExecutor.shutdown();
            }
        }

        Future future = ((ThreadPoolExecutor)THREAD_POOL_REF.get()).submit(runnable);
        FUTURES.add(future);
        return future;
    }

    private static ThreadPoolExecutor createThreadPoolExecutor(Environment environment) {
        int threadPoolCoreSize = CPU_COUNT + 1;
        String coreSizeStr = environment.getProperty("com.alipay.sofa.boot.asyncInitBeanCoreSize");
        if (coreSizeStr != null) {
            threadPoolCoreSize = Integer.parseInt(coreSizeStr);
        }

        int threadPoolMaxSize = CPU_COUNT + 1;
        String maxSizeStr = environment.getProperty("com.alipay.sofa.boot.asyncInitBeanMaxSize");
        if (maxSizeStr != null) {
            threadPoolMaxSize = Integer.parseInt(maxSizeStr);
        }

        SofaLogger.info(String.format("create async-init-bean thread pool, corePoolSize: %d, maxPoolSize: %d.", threadPoolCoreSize, threadPoolMaxSize));
        return new SofaThreadPoolExecutor(threadPoolCoreSize, threadPoolMaxSize, 30L, TimeUnit.SECONDS, new SynchronousQueue(), new NamedThreadFactory("async-init-bean"), new CallerRunsPolicy(), "async-init-bean", "sofa-boot");
    }

    public static void ensureAsyncTasksFinish() {
        Iterator var0 = FUTURES.iterator();

        while(var0.hasNext()) {
            Future future = (Future)var0.next();

            try {
                future.get();
            } catch (Throwable var3) {
                throw new RuntimeException(var3);
            }
        }

        STARTED.set(true);
        FUTURES.clear();
        if (THREAD_POOL_REF.get() != null) {
            ((ThreadPoolExecutor)THREAD_POOL_REF.get()).shutdown();
            THREAD_POOL_REF.set((Object)null);
        }

    }

    public static boolean isStarted() {
        return STARTED.get();
    }
}
